package controller;

import model.Collection;
import model.User;
import validate.CollectionValidate;
import com.jfinal.core.Controller;

import constant.Msg;

public class CollectionController extends Controller{
	public void index(){
		int userId=User.userCache.get(getHeader("token"));
		String recipeid=getPara("recipe_id");
		String validate=null;
		String offset=getPara("offset");
		String length=getPara("length");
		switch (getRequest().getMethod()) {
		case "GET": 
			validate=CollectionValidate.get(offset, length);
			if (validate!=null) {
				renderJson(validate);
				return;
			}
			renderJson(Collection.dao.getCollections(userId, offset, length));
			break;
		case "POST":
			validate=CollectionValidate.post(userId, recipeid);
			if (validate!=null) {
				renderJson(validate);
				return;
			}
			renderJson(Collection.dao.doCollect(userId,recipeid));
			break;
		case "DELETE":
			validate=CollectionValidate.delete(userId,recipeid);
			if (validate!=null) {
				renderJson(validate);
				return;
			}
			renderJson(Collection.dao.cancel(userId, recipeid));
			break;
		default:renderJson(Msg.SUCCESS_EXECUTE);
			break;
		}
	}
}
